require 'apr.net'
require 'apr.uri'
require 'apr.StringBuffer'
require 'apr.oo'
local apr, setmetatable =
      apr, setmetatable
module('apr.net.http')

Browser = apr.oo.Class(function(self, start_uri)
    if start_uri then self:open(start_uri) end
end)

Request = apr.oo.Class(function(self, uri)
    -- Default to a HTTP/1.1 GET request
    self.method = 'GET'; self.version = 1.1; self.body = apr.StringBuffer.new()
    if uri then self:set_uri(uri) end
end)

function Request:set_uri(uri)
    uri = apr.uri.parse(uri)
    uri.scheme = uri.scheme or 'http'
    uri.host = uri.host or 'localhost'
    uri.port = uri.port or 80
    self.uri = uri
    
    self.get = self.uri.query_parsed
end

--- Delegate the request reading and writing to the backend object
function Request:write(...)
    return self.body.write(...)
end

function Request:read(...)
    return self.body.read(...)
end

Response = apr.oo.Class()

CookieJar = {}

Server = apr.oo.Class(function(params)
    return params
end)

function Server:handle_request(request)
    local response = Response.new()
    self.request(request, response)
    return response
end
